package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.Dao;
import com.mohe.dangjian.model.CommunityProject;
import com.mohe.dangjian.service.interfaces.ICommunityProject;
import com.mohe.dangjian.utils.Page;

public class CommunityProjectImpl implements ICommunityProject {

	private Dao<CommunityProject> communityProjectDao;
	
	public Dao<CommunityProject> getCommunityProjectDao() {
		return communityProjectDao;
	}

	public void setCommunityProjectDao(Dao<CommunityProject> communityProjectDao) {
		this.communityProjectDao = communityProjectDao;
	}

	@Override
	public List<CommunityProject> getCommunityProjectList(
			CommunityProject communityProject, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityProject where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 项目名称
		if (!StringUtils.isBlank(communityProject.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityProject.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(communityProject.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + communityProject.getContent() + "%%");
		}
		// 参与人数
		if (communityProject.getAttendNum() != null) {
			where.append(" and attendNum = ?");
			params.add(communityProject.getAttendNum());
		}
		// 备注
		if (!StringUtils.isBlank(communityProject.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + communityProject.getNote() + "%%");
		}
		// 所属居民区
		if (!StringUtils.isBlank(communityProject.getCommunity())) {
			where.append(" and community like ?");
			params.add("%%" + communityProject.getCommunity() + "%%");
		}

		where.append(" order by createDate desc");
		List<CommunityProject> list = communityProjectDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(CommunityProject communityProject) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityProject where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 项目名称
		if (!StringUtils.isBlank(communityProject.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityProject.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(communityProject.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + communityProject.getContent() + "%%");
		}
		// 参与人数
		if (communityProject.getAttendNum() != null) {
			where.append(" and attendNum = ?");
			params.add(communityProject.getAttendNum());
		}
		// 备注
		if (!StringUtils.isBlank(communityProject.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + communityProject.getNote() + "%%");
		}
		// 所属居民区
		if (!StringUtils.isBlank(communityProject.getCommunity())) {
			where.append(" and community like ?");
			params.add("%%" + communityProject.getCommunity() + "%%");
		}

		return communityProjectDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveCommunityProject(CommunityProject communityProject) {
		return communityProjectDao.insert(communityProject);
	}

	@Override
	public boolean saveCommunityProjectList(
			List<CommunityProject> communityProjectList) {
		return communityProjectDao.insert(communityProjectList);
	}

	@Override
	public CommunityProject getCommunityProjectById(Long timeKey) {
		return communityProjectDao.getByKey(timeKey);
	}

	@Override
	public boolean updateCommunityProject(CommunityProject communityProject) {
		return communityProjectDao.update(communityProject);
	}

	@Override
	public boolean delCommunityProject(Long timeKey) {
		return communityProjectDao.delete(timeKey);
	}

	@Override
	public List<CommunityProject> getAllCommunityProject() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return communityProjectDao.query(where.toString(), null);
	}

	@Override
	public List<CommunityProject> getCommunityProjectList(
			CommunityProject communityProject) {
		StringBuffer where = new StringBuffer();
		where.append("from CommunityProject where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 项目名称
		if (!StringUtils.isBlank(communityProject.getName())) {
			where.append(" and name like ?");
			params.add("%%" + communityProject.getName() + "%%");
		}
		// 内容
		if (!StringUtils.isBlank(communityProject.getContent())) {
			where.append(" and content like ?");
			params.add("%%" + communityProject.getContent() + "%%");
		}
		// 参与人数
		if (communityProject.getAttendNum() != null) {
			where.append(" and attendNum = ?");
			params.add(communityProject.getAttendNum());
		}
		// 备注
		if (!StringUtils.isBlank(communityProject.getNote())) {
			where.append(" and note like ?");
			params.add("%%" + communityProject.getNote() + "%%");
		}
		// 所属居民区
		if (!StringUtils.isBlank(communityProject.getCommunity())) {
			where.append(" and community like ?");
			params.add("%%" + communityProject.getCommunity() + "%%");
		}

		where.append(" order by createDate desc");
		List<CommunityProject> list = communityProjectDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

}
